package com.example.contactstest

import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.provider.ContactsContract
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.contentValuesOf
import java.util.jar.Manifest

class MainActivity : AppCompatActivity() {
//    private val contactsList = ArrayList<String>()
//    private lateinit var  adapter: ArrayAdapter<String>
//    private val contactsView: ListView by lazy { findViewById<ListView>(R.id.contactsView) }
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
//
//        adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,contactsList)
//        contactsView.adapter=adapter
////        if( ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS)!=PackageManager.PERMISSION_GRANTED){
////            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS),1)
////        }else{
////            readContacts()
////        }
//        readContacts()
//    }
//
//    private fun readContacts() {
//        //查询联系人信息
//        contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null)?.apply {
//            while (moveToNext()){
//                //获取联系人姓名
//                val displayName = getString(getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
//                //获取联系人手机号
//                val number = getString(getColumnIndex(
//                    ContactsContract.CommonDataKinds.Phone.NUMBER
//                ))
//                contactsList.add("$displayName\n$number")
//            }
//            adapter.notifyDataSetChanged()
//            close()
//        }
//    }

    var ID :String? = null
    private val add: Button by lazy { findViewById<Button>(R.id.add) }
    private val query: Button by lazy { findViewById<Button>(R.id.query) }
    private val update: Button by lazy { findViewById<Button>(R.id.update) }
    private val delete: Button by lazy { findViewById<Button>(R.id.delete) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        add.setOnClickListener{
            val uri = Uri.parse("content://com.example.py.wordlist.WordsProvider")
            val values = contentValuesOf(
            "word" to "addprovider",
            "meaning" to "contentprovider",
            "sample" to "调用contentprovider"
                )
            val newUri = contentResolver.insert(uri,values)
            ID = newUri?.pathSegments?.get(1)
        }
        query.setOnClickListener{
            val uri = Uri.parse("content://com.example.py.wordlist.provider/words")
            contentResolver.query(uri,null,null,null,null)?.apply {
             while (moveToNext()){

             val word = getString(getColumnIndex("word"))
             val meaning = getString(getColumnIndex("meaning"))
                 val sample = getString(getColumnIndex("sample"))
                 Log.i("Main","word is $word")
                 Log.i("Main","meaning is $meaning")
                 Log.i("Main","sample is $sample")


             }
                close()
            }
        }
        update.setOnClickListener{
            ID?.let {
                val uri = Uri.parse("content://com.example.py.wordlist.WordsProvider/words/$it")
                val values = contentValuesOf(
                    "id" to "1",
                    "word" to "addprovider",
                    "meaning" to "contentprovider",
                    "sample" to "调用contentprovider"
                )
                contentResolver.update(uri,values,null,null)
            }
        }
        delete.setOnClickListener{
            ID?.let{
                val uri = Uri.parse("content://com.example.py.wordlist.WordsProvider/words/$it")
                contentResolver.delete(uri,null,null)
            }
        }

    }

}